/*
 ============================================================================
 Name		: FbLeftTabgroup.cpp
 Author	  : ceze
 Version	 : 1.0
 Copyright   : FlashBox for ver.Zhongyou(���ʰ�)
 Description : CFbLeftTabgroup implementation
 ============================================================================
 */

#include "FbTabgroupLeft.h"

#include <EIKENV.H>
#include <stringloader.h>
#include "ConstDefined.hrh"
#include "flashbox4.mbg"
#include "Flashbox4_0x20021B21.rsg"
#include <eiklabel.h>  // for example label control

CFbTabgroupLeft::CFbTabgroupLeft ()
{
	// No implementation required
}

CFbTabgroupLeft::~CFbTabgroupLeft ()
{
	delete m_pBack;
}

CFbTabgroupLeft* CFbTabgroupLeft::NewLC (const TRect& aRect, CCoeControl *pParent )
{
	CFbTabgroupLeft* self = new (ELeave) CFbTabgroupLeft ();
	CleanupStack::PushL (self );
	self->ConstructL (aRect, pParent );
	return self;
}

CFbTabgroupLeft* CFbTabgroupLeft::NewL (const TRect& aRect, CCoeControl *pParent )
{
	CFbTabgroupLeft* self = CFbTabgroupLeft::NewLC (aRect, pParent );
	CleanupStack::Pop (); // self;
	return self;
}

void CFbTabgroupLeft::ConstructL (const TRect& aRect, CCoeControl *pParent )
{
	CFbTabgroup::ConstructL (aRect, pParent );
	
	m_pBack = (CEikonEnv::Static ())->CreateBitmapL (KFlashboxImagePath,
			EMbmFlashbox4Leftground );
}

/**
 * From CoeControl,
 * 按键事件接收处理
 * aKeyEvent ：键盘事件。TKeyEvent 类描述了键盘事件的细节，
 * 				他包括四个属性，分别是iCode, iModifiers, iRepeats, iScanCode 
 * aType ：键盘事件类型，包括：EEventKey, EEventKeyUp or EEventKeyDown
 */
TKeyResponse CFbTabgroupLeft::OfferKeyEventL (const TKeyEvent& aKeyEvent, TEventCode /*aType*/)
{
	TKeyResponse ret = EKeyWasNotConsumed;
	if(m_isDimmedTabs)
		return ret;
	
	TInt indexTemp = m_activeTabIndex;
	switch (aKeyEvent.iCode )
	{
	case EKeyUpArrow://左拨
		indexTemp--;
		if (indexTemp < 0 )
		{
			indexTemp = m_pTabArray->Count () - 1;
		}
		break;
	case EKeyDownArrow://右拨
		indexTemp++;
		if (indexTemp >= m_pTabArray->Count () )
		{
			indexTemp = 0;
		}
		break;
	default:
		return ret;
	}
	//激活新tab
	SetActiveTabByIndexL (indexTemp );
	ret = EKeyWasConsumed;
	return ret;
}

/**
 * 
 */
void CFbTabgroupLeft::NextTabL ()
{
	TInt indexTemp = m_activeTabIndex;
	indexTemp++;
	if (indexTemp >= m_pTabArray->Count () )
	{
		indexTemp = 0;
	}
	//激活新tab
	SetActiveTabByIndexL (indexTemp );
	
}

/**
 * 添加Tab项
 */
void CFbTabgroupLeft::AddTabL (const TInt& aTabId, const TDesC& aResFileName, const TInt& aResImageId, const TInt& aResImageMaskId, const TInt& aResBackId, const TInt & aResTextId )
{
	if (!m_pTabArray )
	{
		m_pTabArray = new (ELeave) CArrayPtrFlat<CFbTabitem> (5 );
	}
	TInt itemIndex = m_pTabArray->Count ();
	
	TPoint aPos (KFbLeftTabitemPosStart.iX, KFbLeftTabitemPosStart.iY
			+ itemIndex * KFbLeftTabitemSize.iHeight );
	TRect aRect (aPos, KFbLeftTabitemSize );
	//创建tab项
	CFbTabitem *pTab = CFbTabitem::NewL (this, aTabId, aResFileName,
			aResImageId, aResImageMaskId, aResBackId, aRect, aResTextId );
	pTab->SetBackColor (KFbSettingRoundRectColor );
	//添加到数组中
	m_pTabArray->AppendL (pTab );
}

/**
 * From CCoeControl, Draw
 * Draw this CFbTabgroup to the screen.
 * @param aRect the rectangle of this view that needs updating
 */
void CFbTabgroupLeft::Draw (const TRect& /*aRect*/) const
{
	// Get the standard graphics context
	CWindowGc& gc = SystemGc ();
	
	TRect drawRect (Rect () );
	/*
	 gc.SetPenStyle (CGraphicsContext::ENullPen );
	 gc.SetBrushColor (KFbTabgroupTopColor );
	 gc.SetBrushStyle (CGraphicsContext::ESolidBrush );
	 gc.DrawRect (drawRect );
	 */
	if (m_pBack )
	{
		gc.DrawBitmap (drawRect, m_pBack );
	}
}

/**
 * 通过序号激活tab项
 * Hightlight (activate) a tab.
 */
void CFbTabgroupLeft::SetActiveTabByIndexL (TInt aTabIndex )
{
	if (aTabIndex < 0 || aTabIndex >= m_pTabArray->Count () )
		return;
	
	CFbTabitem *pTempActiveTab = static_cast<CFbTabitem*> (m_pTabArray->At (aTabIndex ) );
	if (m_activeTabIndex != aTabIndex )
	{
		//初始化LeftTab时
		if(m_activeTabIndex == -1)
		{
			CFbTabitem *pTempTab = static_cast<CFbTabitem*> (m_pTabArray->At (
					m_pTabArray->Count () - 1 ) );
			pTempTab->SetTabImage(KFlashboxImagePath,EMbmFlashbox4Tabmulu2);
		}
		//隐退原激活tab
		if (m_activeTabIndex >= 0 && m_activeTabIndex < m_pTabArray->Count () )
		{
			CFbTabitem *pTempTab = static_cast<CFbTabitem*> (m_pTabArray->At (
					m_activeTabIndex ) );
			pTempTab->SetTabImage(KFlashboxImagePath,EMbmFlashbox4Tabshouchang2+m_activeTabIndex);
			pTempTab->SetItemActiveL (EFalse );
		}
		//激活新tab
		pTempActiveTab = static_cast<CFbTabitem*> (m_pTabArray->At (aTabIndex ) );
		pTempActiveTab->SetTabImage(KFlashboxImagePath,EMbmFlashbox4Tabshouchang + 2*aTabIndex);
		pTempActiveTab->SetItemActiveL (ETrue );
		m_activeTabIndex = aTabIndex;
		//刷新背景
		DrawNow ();
	}
	//发送新tab切换消息
	TInt tabId = pTempActiveTab->TabId ();	
	m_pTabObserver->TabChangedL (m_activeTabIndex, tabId );
	
}

